import 'package:xws/helpers/as_t.dart';
import 'package:xws/models/banner_obj.dart';
import 'package:xws/models/loc_obj.dart';
import 'package:xws/models/report_obj.dart';
import 'package:xws/models/res_obj.dart';
import 'package:xws/utils/app_util.dart';
import 'package:xws/utils/dio_utils/request_apis.dart';
import 'package:xws/utils/dio_utils/request_source.dart';
import 'package:xws/utils/log_util.dart';

/// 获取首页轮播图列表
Future<List<BannerObj>> getHomeBanner({
  int? pageNum,
  int pageSize = 10,
}) async {
  try {
    return await getList<BannerObj>(
      APIS.banner,
      contentType: 'application/json',
      method: 'get',
      dataKey: '',
    );
  } catch (e) {
    error('获取首页轮播图列表出错:$e');

    return <BannerObj>[];
  }
}

/// 获取网络位置
Future<LocObj?> getIpAddr() async {
  try {
    final ResObj? res = await getObj<ResObj>(APIS.ipAddr, method: 'get');

    if (res == null || !res.success) {
      ct(res?.message ?? '网络错误');
      return null;
    }

    return LocObj.fromJson(asT<Map<String, dynamic>>(res.data));
  } catch (e) {
    error('获取网络位置出错:$e');

    return null;
  }
}

/// 举报
Future<bool> reportContent(ReportObj reportObj) async {
  try {
    final ResObj? res = await getObj<ResObj>(
      APIS.reportContent,
      data: reportObj.toJson(),
    );

    if (res == null || !res.success) {
      ct(res?.message ?? '网络错误');
      return false;
    }

    return res.success;
  } catch (e) {
    error('举报出错:$e');
    return false;
  }
}
